
/******************************************************************
  File name:     MNGRBOOT.C
  Belong to:     LASTLINE 2.5 Chinese version
  Date:          8/29/94
  Author:        WangQuan
  Function:      To manager(backup,compare,repare) BOOT RECORD.
  Usage:
		 BACKUP:
		    X:\ANYPATH>Y:\ANYPATH\mngrboot/b<CR>
		 COMPARE:
		    X:\ANYPATH>Y:\ANYPATH\mngrboot/c<CR>
		 REPARE:
		    X:\ANYPATH>Z:mngrboot<CR>
		 HELP:
		    X:\ANYPATH>Y:\ANYPATH\mngrboot/?<CR>

		    X:,Y: = A:,B:,C:,D:...  Z: = A:,B:.
		    BUT C:\LASTLINE\LASTLINE.CFG must be OK.
		    Must in floppy disk when reparing.
  Where stored:  Floppy disk "LASTLINE 2.5 Chinese version
		 Source files"(LASTLINE 2.5 Yingwenban Yuanwenjian)
  Path stored:   \
  OS passed:     DOS 3.3a,DOS 5.0,DOS 6.0,DOS 6.2
  Computer
       passed:   GW286,COMPAQ 386/33(25),AST 286
		 Antai 286 ,At&t 386,DELL 433DE ,NONAME 286
		 Envision 386,so on
*****************************************************************/


/********************************************************/
#include "twingra.h"
#include <fcntl.h>
#include <io.h>
#include <conio.h>
#include <string.h>
#include <bios.h>
#include <dos.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <dir.h>
#define BPS 512
#define HEAD (int)part[i]&0xff
#define SECTOR (int)part[i+1]&0xff
#define TRACK (int)part[i+2]&0xff
#define THISDISKEXIST (((int)part[i]&0xff)!=0)||
		      (((int)part[i+1]&0xff)!=0)||
		      (((int)part[i+2]&0xff)!=0)
#define DISKRESET biosdisk(0,drive,0,0,0,0,0)
int readmainboot(int drive);
int readdosboot(int drive,int head,int sector,int track,int isector);
int writemainboot(int drive);
int writedosboot(int drive,int head,int sector,int track,int isector);
int loadmainboot(int drive);
int loadmainbootagain(int drive);
int loaddosboot(int drive,int isector);
int loaddosbootagain(int drive,int isector);
int savemainboot(int drive);
int savedosboot(int drive,int isector);
int loadparttable(int drive);
int loadparttableagain(int drive);
int readparttable(int way);
void saveparttable(int drive);
void infwq(void);
unsigned check_lock(int drive);
void get_BK(void);
void check_sec(void);
void reboot(void);
void puticon(int lgdrive);
int get_MAY_WRITE_TO_H_SEC(void);
void quit(int i);
void write_series_No(void);
void check_series_No(void);

char part[BPS],boot[BPS];
char dvr[10]="A:*.*";
unsigned  BKTRACK,BKHEAD,MAY_WRITE_TO_H_SEC;
extern int high_of_char,CCLIB;
WINGRA *inf,*icon;
void main(int argc,char *argv[])
 {
   short unsigned int drive=0x80,isector,i,key;
   unsigned char path_of_HZK[30];
   if(argc==1)  /* when reparing */
    strcpy(path_of_HZK,"\\lsth.inf");
   else
    strcpy(path_of_HZK,"c:\\lastline\\lsth.inf");
   dvr[0]=toupper(argv[0][0]);
   title(path_of_HZK);
   if(argc==1 || (!strcmp(strupr(argv[1]),"/B"))
      || (!strcmp(strupr(argv[1]),"/I")))
     wrt_SN_with_fd();
   else
     wrt_SN_with_hd();
   icon=open_win(8,10,12,70,1,0);
   inf=open_win(14,10,24,70,1,1);
   if((argc==2 && strcmp(strupr(argv[1]),"/C")
       && strcmp(strupr(argv[1]),"/B")
       && strcmp(strupr(argv[1]),"/I")
       && strcmp(strupr(argv[1]),"/?"))
       ||(argc>2))
     {
       puthz(icon," {936}. ",14,1);
       high(95,high_of_char*10+5,190,high_of_char*10+7,3,1);
       puthz(inf,
       "    {1065}:    MNGRBOOT [ [/C] or [/B] or [/?] ]\n",14,1);
       quit(1);
     }
   get_BK();
   if(argc==1)
     {
      int sig=0;
      check_sec();
      check_series_No();
      MAY_WRITE_TO_H_SEC=0;
      puthz(icon,"{1130}",14,1);
      high(95,high_of_char*10+5,205,high_of_char*10+7,3,1);
      puticon(0);
      puthz(inf,
      " {1323}?[Y]\b\b",14,1);/*Replace MAIN BOOT RECORD?[Y]\b\b*/
k1:   key=get_ch();
      printf("\x7");
      if(!((toupper(key)=='N')||(toupper(key)=='Y')||(key==13)))
	  goto k1;
      if(toupper(key)!='N')
	{
	  if(loadmainboot(drive)!=-1 && writemainboot(drive)!=-1)
	   {
	    puthz(inf,"\n {1612}.\n",14,1);/*Successful.*/
            sig=1;
	   }
	  else
	    puthz(inf," {1741}.\n",14,1);
	}
      else
	{
	puthz(inf,"N\n",14,1);
	puthz(inf," {1741}.\n",14,1);
	}
      if(!loadparttable(drive))
	readparttable(0);
      i=1;isector=8;
      while((THISDISKEXIST)&&(isector>1))
	{
	  puticon(10-isector);
	  puthz(inf," {1806} ",14,1);
	  boot[0]=75-isector;
	  boot[1]=':';
	  boot[2]=0;
	  puthz(inf,boot,15,1);
	  puthz(inf," {1935}DOS{2000}?[Y]\b\b",14,1);
	  /*Replace DOS BOOT RECORD for
	  logical disk %c:?[Y]\b\b  {1935}DOS{2000}?*/
k2:       key =get_ch();
	  printf("\x7");
	  if(!((toupper(key)=='N')||(toupper(key)=='Y')||(key==13)))
	     goto k2;
	  if(toupper(key)!='N')
	    {
	      if(loaddosboot(drive,isector)!=-1
	      && writedosboot(drive,HEAD,SECTOR,TRACK,isector)!=-1)
		{
		 puthz(inf,"\n {1612}.\n",14,1);/* Successful.*/
                 sig=1;
		}
	      else
                 puthz(inf," {1741}.\n",14,1);
	    }
	  else
	    {
	    puthz(inf,"N\n",14,1);
            puthz(inf," {1741}.\n",14,1);
	    }
	  isector--;i+=3;
	}
      if(sig==1)
	{
	 puthz(inf,"\n{2129}A:{2194}LASTLINE{2291},{2452}.\n",15,1);
	 puthz(inf,"{2677}<Ctrl+Alt+Del>{2710}...",15,1);
	 printf("\x7");
	 reboot();
	}
      else
	quit(0);
   }
   if(!strcmp(strupr(argv[1]),"/C"))
     {
       char tpart[BPS],tboot[BPS];
       int sig=0;
       MAY_WRITE_TO_H_SEC=get_MAY_WRITE_TO_H_SEC();
       puthz(icon,"{2839}",14,1);
       high(100,high_of_char*10+5,200,high_of_char*10+7,3,1);
       puticon(0);
       if(readmainboot(drive)==-1) goto PDOSBOOT;
       memcpy(tpart,part,BPS);
       if(loadmainboot(drive)==-1) goto PDOSBOOT;
       if(memcmp(tpart,part,BPS))
	 {
	  printf("\x7\x7\x7");
	  puthz(inf,"  {3032}!\n",14,1);
	  puthz(inf,"  {1806}?[Y]\b\b",14,1);
	  /*MAIN BOOT RECORD has been changed!*/
	  /*Update with original copy? [Y]*/
kkk:      key=get_ch();
	  printf("\x7");
	  if(!((toupper(key)=='N')||(toupper(key)=='Y')||(key==13)))
	     goto  kkk;
	  if(toupper(key)!='N')
	    {
	      sig=1;
	      if(writemainboot(drive)==-1) goto PDOSBOOT;
	      puthz(inf,"\n  {1612}.\n",14,1);/*Successful.*/
	    }
	  else
	    puthz(inf,"N\n",14,1);
	 }
       else
	puthz(inf,"{3321}.\n",14,1);/*MAIN BOOT RECORD OK.*/
PDOSBOOT:
       if(!loadparttable(drive))
	 readparttable(0);
       i=1;isector=8;
       puthz(inf,"DOS {3546}:\n   ",15,1);
       while((THISDISKEXIST)&&(isector>1))
	 {
	  puticon(10-isector);
	  if(readdosboot(drive,HEAD,SECTOR,TRACK, isector)==-1) goto CONTI;
	  memcpy(tboot,boot,BPS);
	  if(loaddosboot(drive,isector)==-1) goto CONTI;
	  if(memcmp(tboot,boot,BPS))
	   {
	     printf("\x7\x7\x7");
	     puthz(inf,"\n  {3739} ",14,1);
	     tboot[0]=75-isector;
	     tboot[1]=':';
	     tboot[2]=0;
	     puthz(inf,tboot,14,1);
	     puthz(inf," {3836},{1806}?[Y]\b\b",14,1);
	     /*Logical disk %c:     DOS BOOT RECORD
	     has been changed!\n", 75-isector  DOS{3965}*/
	     /*Update with original copy? [Y]*/
k3:          key=get_ch();
	     printf("\x7");
	     if(!((toupper(key)=='N')||(toupper(key)=='Y')||(key==13)))
	       goto k3;
	     if(toupper(key)!='N')
	       {
		 sig=1;
		 if(writedosboot(drive,HEAD,SECTOR,TRACK, isector)==-1)
		 goto CONTI;
		 puthz(inf,"\n  {1612}.",14,1);/*{1612}Successful.*/
	       }
	     else
	       puthz(inf,"N",14,1);
	   }
	  else
	    {
	     tboot[0]=75-isector;
	     tboot[1]=':';
	     tboot[2]=0;
	     puthz(inf,tboot,11,1);
	     puthz(inf,"{4222} ",14,1);
	    }
		  /*{3739} Logical disk %c:
		  DOS BOOT RECORD OK.\n", 75-isector*/
CONTI:
	   isector--;i+=3;
	 }
       if(sig==1)
	{
	 puthz(inf,"\n  {4287},{4352},{4641}.\n",15,1);
	 puthz(inf,"  {2677}<Ctrl+Alt+Del>{2710}...",15,1);
	 /*Now,you should reboot,then check virus for hard disk.*/
				/*Press any key to reboot...*/
         printf("\x7");
	 reboot();
	}
     }

   if((!strcmp(strupr(argv[1]),"/B")) || (!strcmp(strupr(argv[1]),"/I")))
     {
       unsigned char bkuptemp[3]="C:";
       check_sec();
       if(check_lock(drive)==0) quit(1);
       MAY_WRITE_TO_H_SEC=get_MAY_WRITE_TO_H_SEC();
       puthz(icon,"{4866}",14,1);
       high(95,high_of_char*10+5,237,high_of_char*10+7,3,1);
       if(!strcmp(strupr(argv[1]),"/B"))
       {
       puthz(inf,"{5123}.\n",14,1);
       puthz(inf,"{5540}?[Y]\b\b",14,1);
k4:    key=get_ch();
       printf("\x7");
       if(!((toupper(key)=='N')||(toupper(key)=='Y')||(key==13)))
	  goto k4;
       if(toupper(key)=='N')
	  {
	   puthz(inf,"N\n",14,1);
	   quit(1);
	  }
       puthz(inf,"Y\n",14,1);
       }
       puthz(inf,"{5701}...",14,1);
       readmainboot(drive);
       savemainboot(drive);
	 /*Here need not care about value of return.*/
       i=1;isector=8;
       if (!readparttable(1))
	 {
	   puthz(inf,"\n{5830}.\n",14,1);
	   quit(1);
	 }
       saveparttable(drive);
       puthz(inf,"\n   {6119}.",14,1);
       while((THISDISKEXIST)&&(isector>1))
	 {
	   readdosboot(drive,HEAD,SECTOR,TRACK,isector);
	   savedosboot(drive,isector);
	     /*Here need not care about value of return.*/
	   bkuptemp[0]='C'-isector+8;
	   puthz(inf,"\n   ",14,1);
	   puthz(inf,bkuptemp,14,1);
	   puthz(inf,"{6344}DOS{6377}.",14,1);
	   isector--;i+=3;
	 }
       write_series_No();
       puthz(inf,"\n{6570}.",14,1);
       if(!strcmp(strupr(argv[1]),"/B"))
	  quit(0);
       else
	  {
            puthz(inf,"\n\n\n  {4287},{6699}A:{6796},{6925}.",14,1);
	    puthz(inf,"\n{7054},{7119}\"A:,C:\".",14,1);
	    puthz(inf,"\n{2677}<Ctrl+Alt+Del>{2710}...",15,1);
	    printf("\x7");
	    reboot();
	  }
     }

   if(!strcmp(argv[1],"/?"))
     {
       if(check_lock(drive)==0) quit(1);
       puthz(icon,"MNGRBOOT, {7472}",14,1);
       high(95,high_of_char*10+5,317,high_of_char*10+7,3,1);
       puthz(inf,"{1065}:\n",15,1);
       puthz(inf," MNGRBOOT    {1130}",14,1);
       puthz(inf,"     MNGRBOOT/C  {7729}\n",14,1);
       puthz(inf," MNGRBOOT/B  {7922}    ",14,1);
       puthz(inf," MNGRBOOT/?  {8115}",14,1);
       quit(0);
     }
   close(CCLIB);
   cleardevice();
   restorecrtmode();
   exit(0);
 }
int readmainboot(int drive)
  {
    if(biosdisk(2,drive,0,0,1,1,part))
      {
	DISKRESET;
	puthz(inf,"\n{8180}\n",14,1);
	puthz(inf,"{8437}...",15,1);
	printf("\x7");
	get_ch();
	puthz(inf,"\n",14,1);
	printf("\x7");
	return -1;
      }
    return 1;
  }

int readdosboot(int drive,int head,int sector,int track ,int isector)
  {
    if (biosdisk(2,drive,head,track,sector,1,boot))
      {
       DISKRESET;
       puthz(inf,"\n{8630}DOS {8663}\n",14,1);
       puthz(inf,"{8437}...",15,1);
       printf("\x7");
       get_ch();
       puthz(inf,"\n",14,1);
       printf("\x7");
       return -1;
      }
    return 1;
   }


int writemainboot(int drive)
  {
    if(biosdisk(3,drive,0,0,1,1,part))
      {
	DISKRESET;
	puthz(inf,"\n{8856}\n",14,1);
	puthz(inf,"{8437}...",15,1);
	printf("\x7");
	get_ch();
        puthz(inf,"\n",14,1);
	printf("\x7");
	return -1;
      }
    return 1;
  }

int writedosboot(int drive,int head,int sector,int track ,int isector)
  {
    if (biosdisk(3,drive,head,track,sector,1,boot))
      {
       DISKRESET;
       puthz(inf,"\n{9145}DOS {8663}\n",14,1);
       puthz(inf,"{8437}...",15,1);
       printf("\x7");
       get_ch();
       puthz(inf,"\n",14,1);
       printf("\x7");
       return -1;
      }
    return 1;
   }

int loadmainboot(int drive)
  {
    int isfail=0;
    if(biosdisk(2,drive,0,0,9,1,part))
      {
	isfail=1;
	DISKRESET;
	puthz(inf,"\n{9210}\n",14,1);
	puthz(inf,"{9627}...",15,1);
	printf("\x7");
	get_ch();
        puthz(inf,"\n",14,1);
	printf("\x7");
	part[510]=0;
	part[511]=0;
      }
    if((part[510]!='W')||(part[511]!='Q'))
       isfail=1;
    else
       {
	 if(biosdisk(3,drive,BKHEAD,BKTRACK,9,1,part))
	   {
	      DISKRESET;
	      puthz(inf,"\n{9820}\n",14,1);
	      puthz(inf,"{8437}...",15,1);
	      printf("\x7");
	      get_ch();
              puthz(inf,"\n",14,1);
	      printf("\x7");
	    }
	 part[510]=0x55;
	 part[511]=0xAA;
	 return 1;
       }
    if(isfail)
      return(loadmainbootagain(drive));
  }

int loadmainbootagain(int drive)
  {
    if(biosdisk(2,drive,BKHEAD,BKTRACK,9,1,part))
      {
	DISKRESET;
	infwq();
	return -1;
      }
    if((part[510]!='W')||(part[511]!='Q'))
      {
       infwq();
       return -1;
      }
    else
      {
       if(MAY_WRITE_TO_H_SEC==1)
       if(biosdisk(3,drive,0,0,9,1,part))
	 {
	   DISKRESET;
	   puthz(inf,"\n{9820}.\n",14,1);
	   puthz(inf,"{10173},{10494}\n",14,1);
	   puthz(inf,"    C:\\LASTLINE\\MNGRBOOT/C\n",14,1);
	   puthz(inf,"\n{8437}...",15,1);
	   printf("\x7");
	   get_ch();
           puthz(inf,"\n",14,1);
	   printf("\x7");
	 }
       part[510]=0x55;
       part[511]=0xAA;
       return 1;
      }
  }

int loaddosboot(int drive,int isector)
  {
    int isfail=0;
    if (biosdisk(2,drive,0,0,isector,1,boot))
      {
       isfail=1;
       DISKRESET;
       puthz(inf,"\n{10783} DOS {10848}\n",14,1);
       puthz(inf,"{8437}...",15,1);
       printf("\x7");
       get_ch();
       puthz(inf,"\n",14,1);
       printf("\x7");
       boot[3]='B';boot[510]='A';boot[511]='D';
      }
    if((boot[3]!='W')||(boot[510]!='W')||(boot[511]!='Q'))
       isfail=1;
    else
       {
	if(biosdisk(3,drive,BKHEAD,BKTRACK,isector,1,boot))
	   {
	      DISKRESET;
	      puthz(inf,"\n{11169}DOS {11234}\n",14,1);
	      puthz(inf,"{8437}...",15,1);
	      printf("\x7");
	      get_ch();
              puthz(inf,"\n",14,1);
	      printf("\x7");
	    }
	if(((toupper(boot[4]))=='B')&&((toupper(boot[5]))=='M'))
	  boot[3]='I';
	else
	  boot[3]='M';

	boot[510]=0x55;boot[511]=0xAA;
	return 1;
       }
    if(isfail)
       return(loaddosbootagain(drive,isector));
   }


int loaddosbootagain(int drive,int isector)
  {
    if(biosdisk(2,drive,BKHEAD,BKTRACK,isector,1,boot))
      {
	DISKRESET;
	infwq();
	return -1;
      }
    if((boot[3]!='W')||(boot[510]!='W')||(boot[511]!='Q'))
      {
       infwq();
       return -1;
      }
    else
      {
       if(MAY_WRITE_TO_H_SEC==1)
       if(biosdisk(3,drive,0,0,isector,1,boot))
	 {
	   DISKRESET;
	   puthz(inf,"\n{11169}DOS {11234}.\n",14,1);
	   puthz(inf,"{10173},{10494}\n",14,1);
	   puthz(inf,"      C:\\LASTLINE\\MNGRBOOT/B  \n",14,1);
	   puthz(inf,"{8437}...",15,1);
	   printf("\x7");
	   get_ch();
           puthz(inf,"\n",14,1);
	   printf("\x7");
	 }
       if(((toupper(boot[4]))=='B')&&((toupper(boot[5]))=='M'))
	  boot[3]='I';
       else
	  boot[3]='M';
       boot[510]=0x55;
       boot[511]=0xAA;
       return 1;
      }
  }
int savemainboot(int drive)
  {
    part[510]='W';part[511]='Q';
    if(MAY_WRITE_TO_H_SEC==1)
    if(biosdisk(3,drive,0,0,9,1,part))
      {
	DISKRESET;
	puthz(inf,"\n{9820}.\n",14,1);
	puthz(inf,"{8437}...",15,1);
	printf("\x7");
	get_ch();
        puthz(inf,"\n",14,1);
	printf("\x7");
      }
    if(biosdisk(3,drive,BKHEAD,BKTRACK,9,1,part))
      {
	DISKRESET;
	puthz(inf,"\n{9820}.\n",14,1);
	puthz(inf,"{8437}...",15,1);
	printf("\x7");
	get_ch();
        puthz(inf,"\n",14,1);
	printf("\x7");
	return -1;
      }
    return 1;
  }
int savedosboot(int drive,int isector)
  {
    boot[3]='W';boot[510]='W';boot[511]='Q';
    if(MAY_WRITE_TO_H_SEC==1)
    if (biosdisk(3,drive,0,0,isector,1,boot))
      {
       DISKRESET;
       puthz(inf,"\n{11169}DOS {8663}\n",14,1);
       puthz(inf,"{8437}...",15,1);
       printf("\x7");
       get_ch();
       puthz(inf,"\n",14,1);
       printf("\x7");
      }
    if(biosdisk(3,drive,BKHEAD,BKTRACK,isector,1,boot))
      {
       DISKRESET;
       puthz(inf,"\n{11169}DOS {8663}\n",14,1);
       puthz(inf,"{8437}...",15,1);
       printf("\x7");
       get_ch();
       puthz(inf,"\n",14,1);
       printf("\x7");
       return -1;
      }
    return 1;
  }


void saveparttable(int drive)
  {
    part[0]='W';part[511]='Q';
    if(MAY_WRITE_TO_H_SEC==1)
    if(biosdisk(3,drive,0,0,10,1,part))
      {
	DISKRESET;
	puthz(inf,"\n{11491}.\n",14,1);
	puthz(inf,"{8437}...",15,1);
	printf("\x7");
	get_ch();
        puthz(inf,"\n",14,1);
	printf("\x7");
      }
    if(biosdisk(3,drive,BKHEAD,BKTRACK,10,1,part))
      {
	DISKRESET;
	puthz(inf,"\n{11491}.\n",14,1);
	quit(1);
      }
  }

int loadparttable(int drive)   /*1=successful 0=flaure*/
  {
    int isfail=0;
    if(biosdisk(2,drive,0,0,10,1,part))
      {
	isfail=1;
	DISKRESET;
	puthz(inf,"\n{11812}.\n",14,1);
	puthz(inf,"{8437}...",15,1);
	printf("\x7");
	get_ch();
        puthz(inf,"\n",14,1);
	printf("\x7");
	part[0]=0;part[511]=0;
      }
    if((part[0]!='W')||(part[511]!='Q'))
	isfail=1;
    else
      {
	if(biosdisk(3,drive,BKHEAD,BKTRACK,10,1,part))
	   {
	      DISKRESET;
	      puthz(inf,"\n{11491}.\n",14,1);
	      puthz(inf,"{8437}...",15,1);
	      printf("\x7");
	      get_ch();
              puthz(inf,"\n",14,1);
	      printf("\x7");
	    }
	return(1);
      }
    if(isfail)
      return(loadparttableagain(drive));
  }

int loadparttableagain(int drive)   /*1=successful 0=flaure*/
  {
    int back=1;
    if(biosdisk(2,drive,BKHEAD,BKTRACK,10,1,part))
      {
	DISKRESET;
	puthz(inf,"\n{11812}.\n",14,1);
	back=0;
	part[0]=0;part[511]=0;
      }
    if((part[0]!='W')||(part[511]!='Q'))
	back=0;
    else
      {
	if(MAY_WRITE_TO_H_SEC==1)
	if(biosdisk(3,drive,0,0,10,1,part))
	  {
	    DISKRESET;
	    puthz(inf,"\n{11491}.\n",14,1);
	    puthz(inf,"{10173},{10494}\n",14,1);
	    puthz(inf,"      C:\\LASTLINE\\MNGRBOOT/B\n",14,1);
	    puthz(inf,"{8437}...",15,1);
	    printf("\x7");
	    get_ch();
            puthz(inf,"\n",14,1);
	    printf("\x7");
	  }
	back=1;
      }
      return(back);
  }

int readparttable(int way)
  {
    int l=1,drive=0x80,indct=0x1c2;
    unsigned char temp[20],tsec[512];
    if(biosdisk(2,0x80,0,0,1,1,tsec))
      {
	DISKRESET;
	puthz(inf,"{12133}.\n",14,1);
	quit(1);
      }
    while(indct<=0x1f2)
      {
	if((tsec[indct]==1)||(tsec[indct]==4)||(tsec[indct]==6))
	  {
	    part[l]=tsec[indct-3]&0xff;
	    part[l+1]=tsec[indct-2]&0xff;
	    part[l+2]=tsec[indct-1]&0xff;
	    l+=3;
	    break;
	  }
	else
	  indct += 0x10;
      }
    indct=0x1c2;
    temp[0]=temp[1]=temp[2]=0;
    while(indct<=0x1f2)
      {
	if(tsec[indct]==5)
	  {
	    temp[0]=tsec[indct-3]&0xff;
	    temp[1]=tsec[indct-2]&0xff;
	    temp[2]=tsec[indct-1]&0xff;
	    break;
	  }
	else
	  indct += 0x10;
      }
    while((temp[0]!=0)||(temp[1]!=0)||(temp[2]!=0))
      {
	 if(biosdisk(2,0x80,temp[0],temp[2],temp[1],1,tsec))
	   {
	     DISKRESET;
             puthz(inf,"{12133}.\n",14,1);
	     quit(1);
	   }
	 part[l]=tsec[0x1BF]&0xff;
	 part[l+1]=tsec[0x1C0]&0xff;
	 part[l+2]=tsec[0x1C1]&0xff;
	 temp[0]=tsec[0x1CF]&0xff;
	 temp[1]=tsec[0x1D0]&0xff;
	 temp[2]=tsec[0x1D1]&0xff;
	 l+=3;
      }
    part[l]=part[l+1]=part[l+2]=0;
    return(1);
 }

void infwq(void)
 {
   static unsigned flag=0;
   if(flag==0)
   {
   flag=1;
   printf("\x7\x7\x7",14,1);
   inf=open_win(14,10,24,70,1,1);
   clr_win(inf,1);
   puthz(inf,"   LASTLINE{12326},{12455}LASTLINE{12616}:\n",14,1);
   puthz(inf,"          MNGRBOOT/B ",14,1);
   puthz(inf,"\n{8437}...",15,1);
   get_ch();
   printf("\x7");
   }
   puthz(inf,"\n",14,1);
 }
unsigned check_lock(int drive)
  {
    int i;
    FILE *fp;
    char end_inf[20];
    if((fp=fopen("c:\\lastline\\lastline.cfg","rb"))==NULL)
      {
	puthz(inf,"\n{12809}.",14,1);
	quit(1);
      }
    fread(end_inf,3,1,fp);
    fclose(fp);
    for (i=0;i<3;i++)
       end_inf[i]=~(end_inf[i]);
    if(biosdisk(2,drive,0,0,1,1,boot))
      {
	biosdisk(0,drive,0,0,0,0,0);
	puthz(inf,"\n{12970}",14,1);
	quit(1);
      }
    if((boot[0x1c3]==end_inf[0])&&(boot[0x1c4]==end_inf[1])
    &&(boot[0x1c5]==end_inf[2]))
      return (1);
    else
      {
	puthz(inf,"\n{13035}.",14,1);
	quit(1);
      }
    return(0);
    }
void get_BK(void)
 {
   unsigned char tttt[512];
   unsigned HD_base_table_adr[4],temp[3],a=0,b=0x400;
   HD_base_table_adr[0]=peekb(0,0x104)&0xff;
   HD_base_table_adr[1]=peekb(0,0x105)&0xff;
   HD_base_table_adr[2]=peekb(0,0x106)&0xff;
   HD_base_table_adr[3]=peekb(0,0x107)&0xff;
   temp[0]=peekb((HD_base_table_adr[3]<<8)+HD_base_table_adr[2],
   (HD_base_table_adr[1]<<8)+HD_base_table_adr[0]+0);
   temp[1]=peekb((HD_base_table_adr[3]<<8)+HD_base_table_adr[2],
   (HD_base_table_adr[1]<<8)+HD_base_table_adr[0]+1);
   temp[2]=peekb((HD_base_table_adr[3]<<8)+HD_base_table_adr[2],
   (HD_base_table_adr[1]<<8)+HD_base_table_adr[0]+2);
   BKTRACK=((temp[1]&0xff)<<8)+(temp[0]&0xff)-1;
/*   BKHEAD=(temp[2]&0xff)-1;*/
   BKHEAD=0;
   if(BKTRACK > 0x3FF)
    {
      BKTRACK=0x3FF;BKTRACK=0;
      if(biosdisk(2,0x80,BKHEAD,BKTRACK,1,1,tttt))
       {
	BKTRACK=0x200;
	while( (b-a)>1 )
	 {
	   if(biosdisk(2,0x80,BKHEAD,BKTRACK,1,1,tttt))
	      b=BKTRACK;
	   else
	   a=BKTRACK;
	   BKTRACK=(a+b)/2;
	 }
       }
    }
}


void check_sec(void)
 {
   struct ffblk ffb;
   findfirst(dvr,&ffb,FA_LABEL);
   if(strcmp(ffb.ff_name,"WQLASTLI.NE"))
   {
/*     biosdisk(3,toupper(dvr[0])-'A',0,0,1,0xff,part+0x91);*/
     puthz(inf,"\n{13035}.",14,1);
     quit(1);
    }
   if(absread(toupper(dvr[0])-'A',1,0,boot))
    if(absread(toupper(dvr[0])-'A',1,0,boot))
     if(absread(toupper(dvr[0])-'A',1,0,boot))
     {
       biosdisk(0,toupper(dvr[0])-'A',0,0,0,0,0);
       puthz(inf,"\n{12970}.",14,1);
       quit(1);
     }
   if(absread(toupper(dvr[0])-'A',1,2000,part))
    if(absread(toupper(dvr[0])-'A',1,2000,part))
     if(absread(toupper(dvr[0])-'A',1,2000,part))
     {
       biosdisk(0,toupper(dvr[0])-'A',0,0,0,0,0);
       puthz(inf,"\n{12970}.",14,1);
       quit(1);
     }
   if(memcmp(boot,part,512))
    {
/*     biosdisk(3,toupper(dvr[0])-'A',0,0,1,0xff,part+0x91);*/
     puthz(inf,"\n{13035}.",14,1);
     quit(1);
    }
 }
int getdosnum(void)
 {
      union REGS in,out;
      in.h.ah=0x30;
      intdos(&in,&out);
      return(out.h.al);
 }
void reboot(void)
 {
/*     union REGS in,out;*/
     close(CCLIB);
/*     cleardevice();
     restorecrtmode();
     int86(0x19,&in,&out);*/
     while(1);
 }

void puticon(int lgdrive)/* lgdrive=0,put main boot record icon */
 {
   int i;
   char a[2]="C";
   static int x=220;
   setfillstyle(1,1);
   high(x,high_of_char*8+2,x+30,high_of_char*10,1,1);
   nohigh(x+1,high_of_char*8+3,x+29,high_of_char*10-1,1,1);
   setcolor((lgdrive==0)?15:(16-lgdrive));
   for (i=high_of_char*9+4;i>high_of_char*9-6;i-=2)
   fillellipse(x+15,i,8,4);
   fillellipse(x+15,i+2,4,2);
   setfillstyle(1,15);
   if(lgdrive!=0)
     {
       a[0]=lgdrive+65;
       outtextxy(x+13,high_of_char*10+5,a);
     }
   else
     outtextxy(x,high_of_char*10+5,"MAIN");
   x+=40;
 }
int get_MAY_WRITE_TO_H_SEC(void)
 {
   FILE *fp;
   unsigned char result;
   if((fp=fopen("c:\\lastline\\lastline.cfg","rb"))==NULL)
      {
	puthz(inf,"{12809}.",14,1);
	quit(1);
      }
    fseek(fp,500,SEEK_SET);
    fread(&result,1,1,fp);
    fclose(fp);
    return result&0xff;
 }

void quit(int i)
{

 printf("\x7");
 puthz(inf,"\n {13516}...",15,1);
 close(CCLIB);
 get_ch();
 printf("\x7");
 cleardevice();
 restorecrtmode();
 exit(i);
}

void write_series_No(void)
{
   unsigned char part[512],boot[512];
   if(biosdisk(2,0x80,BKHEAD,BKTRACK,10,1,part))
    {
     puthz(inf,"\n{13709}.",14,1);
     quit(1);
    }
   if(absread(toupper(dvr[0])-65,1,0,boot))
    if(absread(toupper(dvr[0])-65,1,0,boot))
     if(absread(toupper(dvr[0])-65,1,0,boot))
     {
       biosdisk(0,toupper(dvr[0])-65,0,0,0,0,0);
       puthz(inf,"\n{13709}.",14,1);
       quit(1);
     }
   part[507]=boot[0x27]&0xFF;
   part[508]=boot[0x28]&0xFF;
   part[509]=boot[0x29]&0xFF;
   part[510]=boot[0x2a]&0xFF;
   if(MAY_WRITE_TO_H_SEC==1)
    if(biosdisk(3,0x80,0,0,10,1,part))
    {
     puthz(inf,"\n{13709}.",14,1);
     quit(1);
    }
   if(biosdisk(3,0x80,BKHEAD,BKTRACK,10,1,part))
    {
     puthz(inf,"\n{13709}.",14,1);
     quit(1);
    }
}


void check_series_No(void)
{
   unsigned char part[512],boot[512];
   if(absread(toupper(dvr[0])-65,1,0,boot))
    if(absread(toupper(dvr[0])-65,1,0,boot))
     if(absread(toupper(dvr[0])-65,1,0,boot))
     {
       biosdisk(0,toupper(dvr[0])-65,0,0,0,0,0);
       puthz(inf,"\n{13934}.",14,1);
       quit(1);
     }
   if(biosdisk(2,0x80,BKHEAD,BKTRACK,10,1,part))
    if(biosdisk(2,0x80,0,0,10,1,part))
    {
     puthz(inf,"\n{13934}.",14,1);
     quit(1);
    }
   if(part[0]!='W' || part[511]!='Q')
    {
     if(biosdisk(2,0x80,0,0,10,1,part))
      {
	puthz(inf,"\n{13934}.",14,1);
	quit(1);
      }
    }
   if(    part[0]=='W'
       && part[511]=='Q'
       &&
       (  part[507] != boot[0x27]
       || part[508] != boot[0x28]
       || part[509] != boot[0x29]
       || part[510] != boot[0x2a]
       )
     )
     {
       puthz(inf,"\n{14127}.",14,1);
       quit(1);
     }
}

unsigned get_SN_from_fd(void)
{
  unsigned SN;
  unsigned char part[512];
  if(absread(toupper(dvr[0])-65,1,0,part))
    if(absread(toupper(dvr[0])-65,1,0,part))
     if(absread(toupper(dvr[0])-65,1,0,part))
       ;
  SN = part[0x27]+part[0x28]*256;
  return SN;
}